package web.controller;

import com.alibaba.fastjson.JSONObject;
import common.bean.SearchPageResult;
import common.bean.SearchParameter;
import common.tag.easyui.DataGrid;
import common.tag.easyui.TagUtils;
import module.model.*;
import module.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping(value = "/admin/assessment")
public class AssessmentController extends BaseController{
    @Autowired
    private EvaluationFormService evaluationFormService;

    @Autowired
    private AssessmentDataService assessmentDataService;

    @Autowired
    private AssessmentItemService assessmentItemService;

    @Autowired
    private AssessmentStandardValueService assessmentStandardValueService;
    @Autowired
    private AssessmentGradingService assessmentGradingService;

    public final static String prefix="admin/module/assessment/";

    protected String getPrefix() {
        return prefix;
    }
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public void list(DataGrid dataGrid){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPageIdx(page);
        SearchPageResult<EvaluationFormModel> serviceModelSearchPageResult = evaluationFormService.getEvaluationFormModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping(value = "form",method = RequestMethod.GET)
    public String form(Long formId,ModelMap map){
        if(formId!=null){
            map.put("evaluationFormModel",evaluationFormService.getEvaluationFormModelByFormId(formId));
        }

        return prefix+"form";
    }

    @RequestMapping(value = "dataform",method = RequestMethod.GET)
    public String dataform(Long dataId,ModelMap map){
        if(dataId!=null){
            map.put("assessmentDataModel",assessmentDataService.getAssessmentDataModelByDataId(dataId));
        }

        return prefix+"dataform";
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(EvaluationFormModel evaluationFormModel){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            if(evaluationFormModel.getFormId()==null){
                evaluationFormModel.setCreateUserId(getCurAdminId());
            }else{
                evaluationFormModel.setLastUpdateUserId(getCurAdminId());
            }
            evaluationFormService.saveEvaluationFormModel(evaluationFormModel);
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "datasave",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap savedata(AssessmentDataModel assessmentDataModel){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            if(assessmentDataModel.getDataId()==null){
                assessmentDataModel.setCreateUserId(getCurAdminId());
            }
            assessmentDataService.saveAssessmentDataModel(assessmentDataModel);
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "dataList",method = RequestMethod.GET)
    public String dataList(ModelMap map,Long formId){
        map.put("evaluationFormModel",evaluationFormService.getEvaluationFormModelByFormId(formId));
        return prefix+"dataList";
    }

    @RequestMapping(value = "dataList",method = RequestMethod.POST)
    @ResponseBody
    public void dataList(DataGrid dataGrid,Long formId){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPageIdx(page);
        searchParameter.addParam("formId",formId);
        SearchPageResult<AssessmentDataModel> serviceModelSearchPageResult = assessmentDataService.getAssessmentDataModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "itemList",method = RequestMethod.GET)
    public String itemList(ModelMap map,Long dataId){
        map.put("assessmentDataModel",assessmentDataService.getAssessmentDataModelByDataId(dataId));
        return prefix+"itemList";
    }

    @RequestMapping(value = "itemList",method = RequestMethod.POST)
    @ResponseBody
    public void itemList(DataGrid dataGrid,Long dataId){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPageIdx(page);
        searchParameter.addParam("dataId",dataId);
        SearchPageResult<AssessmentItemModel> serviceModelSearchPageResult = assessmentItemService.getAssessmentItemModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "itemform",method = RequestMethod.GET)
    public String itemform(Long itemId,ModelMap map){
        if(itemId!=null){
            map.put("assessmentItemModel",assessmentItemService.getAssessmentItemModelByItemId(itemId));
        }

        return prefix+"itemform";
    }

    @RequestMapping(value = "itemsave",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap itemsave(AssessmentItemModel assessmentItemModel){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            if(assessmentItemModel.getItemId()==null){
                assessmentItemModel.setCreateUserId(getCurAdminId());
            }
            assessmentItemService.saveAssessmentItemModel(assessmentItemModel);
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "valueList",method = RequestMethod.GET)
    public String valueList(ModelMap map,Long dataId){
        map.put("assessmentDataModel",assessmentDataService.getAssessmentDataModelByDataId(dataId));
        return prefix+"valueList";
    }

    @RequestMapping(value = "valueList",method = RequestMethod.POST)
    @ResponseBody
    public void valueList(DataGrid dataGrid,Long dataId){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPageIdx(page);
        searchParameter.addParam("dataId",dataId);
        SearchPageResult<AssessmentStandardValueModel> serviceModelSearchPageResult = assessmentStandardValueService.getAssessmentStandardValueModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "valueform",method = RequestMethod.GET)
    public String valueform(Long valueId,ModelMap map){
        if(valueId!=null){
            map.put("assessmentStandardValueModel",assessmentStandardValueService.getAssessmentStandardValueModelByValueId(valueId));
        }

        return prefix+"valueform";
    }

    @RequestMapping(value = "valuesave",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap valuesave(AssessmentStandardValueModel assessmentStandardValueModel){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            if(assessmentStandardValueModel.getStandardValueId()==null){
                assessmentStandardValueModel.setCreateUserId(getCurAdminId());
            }
            assessmentStandardValueService.saveAssessmentStandardValue(assessmentStandardValueModel);
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping(value = "gradingList",method = RequestMethod.GET)
    public String gradingList(ModelMap map,Long dataId,Long formId){
        if(dataId!=null) {
            map.put("assessmentDataModel", assessmentDataService.getAssessmentDataModelByDataId(dataId));
        }
        if(formId!=null){
            map.put("evaluationFormModel",evaluationFormService.getEvaluationFormModelByFormId(formId));
        }
        return prefix+"gradingList";
    }

    @RequestMapping(value = "gradingList",method = RequestMethod.POST)
    @ResponseBody
    public void gradingList(DataGrid dataGrid,Long dataId,Long formId){
        int page = dataGrid.getPage();
        SearchParameter searchParameter = new SearchParameter();
        searchParameter.setPageIdx(page);
        searchParameter.addParam("objId",dataId!=null ? dataId : formId);
        searchParameter.addParam("gradingType",dataId!=null ? 2 : 1);
        SearchPageResult<AssessmentGradingModel> serviceModelSearchPageResult = assessmentGradingService.getAssessmentGradingModelListPage(searchParameter);
        dataGrid.setResults(serviceModelSearchPageResult.getResult());
        dataGrid.setTotal(serviceModelSearchPageResult.getTotal());
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Cache-Control", "no-store");
        JSONObject object = TagUtils.getJson(dataGrid);
        try {
            PrintWriter pw=response.getWriter();
            pw.write(object.toString());
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "gradingForm",method = RequestMethod.GET)
    public String gradingForm(Long gradingId,ModelMap map){
        if(gradingId!=null){
            map.put("assessmentGradingModel",assessmentGradingService.getAssessmentGradingModelByGradingId(gradingId));
        }

        return prefix+"gradingForm";
    }

    @RequestMapping(value = "gradingSave",method = RequestMethod.POST)
    @ResponseBody
    public ModelMap gradingSave(AssessmentGradingModel assessmentGradingModel){
        ModelMap map = new ModelMap();
        setFailure(map);
        try{
            if(assessmentGradingModel.getGradingId()==null){
                assessmentGradingModel.setCreateUserId(getCurAdminId());
            }
            assessmentGradingService.saveAssessmentGradingModel(assessmentGradingModel);
            setSuccess(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
}
